package com.zoweunion.mechanic.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtils {

    public static SimpleDateFormat localFormater = new SimpleDateFormat("yyyy-MM-dd");

    public static SimpleDateFormat localFormater_One = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static List<String> getDays(String startTime, String endTime) {
        // 返回的日期集合
        List<String> days = new ArrayList<String>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return days;
    }

    public static List<Date> getDateDays(Date startTime, Date endTime) {
        // 返回的日期集合
        List<Date> days = new ArrayList<Date>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start = startTime;
        Date end = endTime;
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
        System.out.println(localFormater_One.format(tempStart.getTime()));
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        tempEnd.add(Calendar.DATE, +0);// 日期加1(包含结束)
        while (tempStart.before(tempEnd)) {
            days.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }

        return days;
    }

    // 返回时间格式如：2020-02-17 00:00:00
    public static Long getStartOfDay(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime().getTime();
    }

    public static Date getStartOfDayNew(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    // 返回时间格式如：2020-02-19 23:59:59
    public static Long getEndOfDay(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime().getTime();
    }

    public static Date getEndOfDayNew(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }

    public static void main(String[] args) throws ParseException {
        Date startTimeParm = localFormater_One.parse("2023-08-08 09:00:00");
        Date endTimeParm = localFormater_One.parse("2023-08-08 19:59:59");
        List<Date> dateList = DateUtils.getDateDays(startTimeParm, endTimeParm);
        int dateListSize = dateList.size();
        System.out.println("dateListSize" + dateListSize);
        for (int i = 0; i < dateListSize; i++) {
            Date dateOne = dateList.get(i);
            if (dateListSize == 1) {
                System.out.println("startTimeNew" + localFormater_One.format(dateOne));
                System.out.println("endTimeNew" + localFormater_One.format(endTimeParm));
            } else {
                if (i == 0) {
                    System.out.println("startTimeNew : " + localFormater_One.format(dateOne));
                    System.out.println("endTimeNew   : " + localFormater_One.format(DateUtils.getEndOfDayNew(dateOne)));
                    System.out.println("-------------");
                } else if (i > 0 && i <dateListSize-1 ) {
                    System.out.println("startTimeNew : " + localFormater_One.format(DateUtils.getStartOfDayNew(dateOne)));
                    System.out.println("endTimeNew   : " + localFormater_One.format(DateUtils.getEndOfDayNew(dateOne)));
                    System.out.println("-------------");
                } else if (i == dateListSize - 1) {
                    System.out.println("startTimeNew : " + localFormater_One.format(DateUtils.getStartOfDayNew(dateOne)));
                    System.out.println("endTimeNew   : " + localFormater_One.format(endTimeParm));
                    System.out.println("-------------");
                }
            }

        }

    }
}